import { BasicColumn, FormSchema } from '/@/components/Table';
import { h, ref } from 'vue';
import { Tag } from 'ant-design-vue';
import dayjs from 'dayjs';
const dates = ref();
export const columns: BasicColumn[] = [
  {
    title: '姓名',
    dataIndex: 'name',
    width: 120,
    align: 'center',
  },
  {
    title: '学生卡号',
    dataIndex: 'stid',
    align: 'center',
    width: 120,
  },
  {
    title: '一卡通账户',
    dataIndex: 'account',
    align: 'center',
    width: 120,
  },
  {
    title: '消费开始时间',
    dataIndex: 'stime',
    align: 'center',
    width: 180,
  },
  {
    title: '消费结束时间',
    dataIndex: 'etime',
    align: 'center',
    width: 180,
  },
  {
    title: '消费金额(分)',
    dataIndex: 'money',
    align: 'center',
    width: 120,
  },
  {
    title: '获取时间',
    dataIndex: 'ctime',
    align: 'center',
    width: 180,
  },
  {
    title: '扣费状态',
    dataIndex: 'state',
    align: 'center',
    width: 120,
    customRender: ({ text }) => {
      return h(Tag, { color: text == 1 ? 'red' : text == 2 ? 'green' : 'gray' }, () =>
        text == 1 ? '待扣费' : text == 2 ? '已扣费' : text == 0 ? '无效记录' : '--'
      );
    },
  },
  {
    title: '卡状态',
    dataIndex: 'cardState',
    align: 'center',
    width: 120,
    customRender: ({ text }) => {
      return h(Tag, { color: text == 1 ? 'green' : text == 2 ? 'red' : text == 0 ? 'orange' : 'gray' }, () =>
        text == 1 ? '正常' : text == 2 ? '黑名单' : text == 0 ? '特殊卡' : text == -2 ? '挂失卡' : '--'
      );
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '姓名',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'stid',
    label: '学生卡号',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'account',
    label: '一卡通账号',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'money',
    label: '',
    component: 'Input',
    show: false,
  },
  {
    field: 'compareSig',
    label: '',
    component: 'Input',
    show: false,
  },
  {
    field: 'moneyFormat',
    label: '消费金额',
    component: 'SelectAndInput',
    componentProps: ({ formActionType }) => {
      return {
        onChange: (e) => {
          const { setFieldsValue } = formActionType;
          if (e) {
            const arr = e?.split(',');
            if (arr?.[1] || arr?.[1] === 0) {
              setFieldsValue({ money: arr[1], compareSig: arr?.[0] || 0 });
            } else {
              setFieldsValue({ money: undefined, compareSig: undefined });
            }
          } else {
            setFieldsValue({ money: undefined, compareSig: undefined });
          }
          setFieldsValue({ moneyFormat: undefined });
        },
      };
    },
    colProps: { span: 6 },
  },
  {
    field: 'rtime',
    label: '消费时间',
    component: 'RangePicker',
    colProps: { span: 6 },
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: ['请选择开始日期时间', '请选择结束日期时间'],
      allowClear: false,
      disabledDate: (current) => {
        if (!dates.value || (dates.value as any).length === 0) {
          return false;
        }
        const tooLate = dates.value[0] && current.diff(dates.value[0], 'month') > 6;
        const tooEarly = dates.value[1] && dayjs(dates.value[1]).diff(current, 'month') > 6;
        return tooEarly || tooLate;
      },
      onCalendarChange: (val) => {
        dates.value = val;
      },
    },
    defaultValue: [
      dayjs().subtract(30, 'day').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
      dayjs().subtract(1, 'day').endOf('day').format('YYYY-MM-DD HH:mm:ss'),
    ],
  },
  {
    field: 'state',
    label: '消费状态',
    component: 'RadioGroup',
    componentProps: {
      //options里面由一个一个的radio组成,支持disabled禁用
      options: [
        { label: '待扣费', value: 1 },
        { label: '已结算', value: 2 },
        { label: '已失效', value: 0 },
      ],
    },
    colProps: { span: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    label: '主键',
    field: 'idx',
    component: 'Input',
    show: false,
  },
  {
    label: '学生姓名',
    field: 'name',
    component: 'Input',
  },
  {
    label: '学生卡号',
    field: 'stid',
    component: 'Input',
  },
  {
    label: '卡号硬件ID',
    field: 'cid',
    component: 'Input',
  },
  {
    label: '一卡通账户',
    field: 'account',
    component: 'Input',
  },
  {
    label: '身份证号',
    field: 'idcard',
    component: 'Input',
  },
  {
    label: '生效时间',
    field: 'stime',
    component: 'DatePicker',
    componentProps: {
      //日期格式化，页面上显示的值
      format: 'YYYY-MM-DD HH:mm:ss',
      //返回值格式化（绑定值的格式）
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      //是否显示今天按钮
      showToday: true,
      showTime: true,
    },
  },
  {
    label: '失效时间',
    field: 'etime',
    component: 'DatePicker',
    componentProps: {
      //日期格式化，页面上显示的值
      format: 'YYYY-MM-DD HH:mm:ss',
      //返回值格式化（绑定值的格式）
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      //是否显示今天按钮
      showToday: true,
      showTime: true,
    },
  },
  {
    label: '当前状态',
    field: 'state',
    component: 'RadioButtonGroup',
    componentProps: {
      //options里面由一个一个的radio组成,支持disabled禁用
      options: [
        { label: '有效', value: 1 },
        { label: '黑名单', value: -2 },
      ],
    },
  },
];
